package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * Created by yls on 2018/7/3.
 */

public class TempratureView extends View {
    private Paint paint;
    private Temprature[] tempratures;
    private Point[] highPoints = new Point[5];
    private Point[] lowPoints= new Point[5];;

    public TempratureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.CYAN);
        super.onDraw(canvas);

        Path highPath = new Path();
        for(int i=0; i<highPoints.length;i++){
            if(i==0){
                highPath.moveTo(highPoints[i].x, highPoints[i].y);
            }else {
                highPath.lineTo(highPoints[i].x, highPoints[i].y);
            }
            paint.setStrokeWidth(10);
            canvas.drawPoint(highPoints[i].x,highPoints[i].y, paint);

        }

        paint.setStrokeWidth(5);
        canvas.drawPath(highPath,paint);

        Path lowPath = new Path();
        for(int i=0; i<lowPoints.length;i++){
            if(i==0){
                lowPath.moveTo(lowPoints[i].x, lowPoints[i].y);
            }else {
                lowPath.lineTo(lowPoints[i].x, lowPoints[i].y);
            }
            paint.setStrokeWidth(10);
            canvas.drawPoint(lowPoints[i].x,lowPoints[i].y, paint);

        }

        paint.setStrokeWidth(5);
        canvas.drawPath(lowPath,paint);





    }

    public void setData(List<WeatherData.DataBean.ForecastBean> forecast) {

        tempratures = new Temprature[5];
        tempratures[0] = new Temprature(Integer.parseInt(forecast.get(0).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(0).getLow().substring(3,5)));
        tempratures[1] = new Temprature(Integer.parseInt(forecast.get(1).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(1).getLow().substring(3,5)));
        tempratures[2] = new Temprature(Integer.parseInt(forecast.get(2).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(2).getLow().substring(3,5)));
        tempratures[3] = new Temprature(Integer.parseInt(forecast.get(3).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(3).getLow().substring(3,5)));
        tempratures[4] = new Temprature(Integer.parseInt(forecast.get(4).getHigh().substring(3,5)),
                Integer.parseInt(forecast.get(4).getLow().substring(3,5)));


        for(int i=0; i<tempratures.length;i++){
            highPoints[i] = new Point(100*(i+1), (50 - tempratures[i].high) * 20);
            lowPoints[i] = new Point(100*(i+1), (50 - tempratures[i].low) * 20);
        }

    }
}
